﻿
#ifndef __SPK_ULOG_H_
#define __SPK_ULOG_H_

#include <stdint.h>
#include <stdarg.h>

/* logger level, the number is compatible for syslog */
#define LOG_LVL_ASSERT                 0
#define LOG_LVL_ERROR                  3
#define LOG_LVL_WARNING                4
#define LOG_LVL_INFO                   6
#define LOG_LVL_DBG                    7
#define ULOG_LINE_BUF_SIZE             1024
#define ULOG_NEWLINE_SIGN              "\r\n"


#if !defined(LOG_TAG)
#define LOG_TAG                        "NO_TAG"
#endif 

#if !defined(LOG_LVL)
#define LOG_LVL                         LOG_LVL_DBG
#endif

void ulog_output(uint32_t level, const char* tag, const char* format, ...);

#if (LOG_LVL >= LOG_LVL_DBG)
#define LOG_D(...)           ulog_output(LOG_LVL_DBG, LOG_TAG, __VA_ARGS__)
#else
#define LOG_D(...)
#endif 

#if (LOG_LVL >= LOG_LVL_INFO)
#define LOG_I(...)           ulog_output(LOG_LVL_INFO, LOG_TAG,__VA_ARGS__)
#else
#define LOG_I(TAG, ...)
#endif 

#if (LOG_LVL >= LOG_LVL_WARNING)
 #define LOG_W(...)           ulog_output(LOG_LVL_WARNING, LOG_TAG, __VA_ARGS__)
#else
 #define LOG_W(...)
#endif

#if (LOG_LVL >= LOG_LVL_ERROR)
 #define LOG_E(...)           ulog_output(LOG_LVL_ERROR, LOG_TAG,__VA_ARGS__)
#else
#define LOG_E(...)
#endif 


#define RT_ASSERT(EX)                                                         \
if (!(EX))                                                                    \
{                                                                             \
    LOG_E("(%s) assert err: %s, line:%d \n", #EX, __FUNCTION__, __LINE__); \
    while(1);\
}

#if (LOG_LVL >= LOG_LVL_DBG)

#define SPK_RUN_TIME(_NAME_,_FUNC_)    {\
											LARGE_INTEGER _func_start_time_;\
											LARGE_INTEGER _func_end_time_;\
											LARGE_INTEGER SYS_CLK;\
											QueryPerformanceCounter(&_func_start_time_);\
											_FUNC_\
											QueryPerformanceCounter(&_func_end_time_);\
											QueryPerformanceFrequency(&SYS_CLK); \
											double __ms__  = 1000.0*(_func_end_time_.QuadPart - _func_start_time_.QuadPart)/(SYS_CLK.QuadPart);\
											if(__ms__ < 1.0) LOG_I("%s: run time %.1f us",#_NAME_, __ms__*1000);\
											else if(__ms__ < 1000.0) LOG_I("%s: run time %.1f ms", #_NAME_,__ms__);\
											else LOG_I("%s: run time %.1f s",#_NAME_, __ms__/1000.0);\
									   }
#else 
#define SPK_RUN_TIME(_NAME_,_FUNC_)    _FUNC_
#endif 

#endif 

